import 'package:flutter/material.dart';
import '../../helper/Helper.dart';
import '../../helper/uidata.dart';
import '../../model/swxg.dart';
import '../../model/xhnl.dart';

import '../../helper/channelHelper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

class ResXLNLPage extends StatefulWidget {
  @override
  ResXLNLPage();

  @override
  ResXLNLPageState createState() => new ResXLNLPageState();
}

class ResXLNLPageState extends State<ResXLNLPage> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();

  late XHNL x1;
  bool ifOK = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //print(UIData.SkData1.toJson());
    if (UIData.SkData1 != null) {
      c1.text = Helper.RoundDouble(UIData.SkData1!.H1, 2, "");
    } else {
      c1.text = "850";
    }

    c2.text = "0.5";
    c3.text = "1.0";
    c4.text = "76.0";

    // setQ();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: Text("泄洪流量计算")),
      // floatingActionButton: getFloatingButton(context),
      body: ResXLNLBody(context),
    );
  }

  void setQ() async {
    try {
      ifOK = false;
      x1 = await XHNL.Get(c1.text, c2.text, c3.text, c4.text) as XHNL;
      ifOK = true;
      this.setState(() => {});
      ChannelHelper.showToast("计算完成！");
    } catch (e) {
      ChannelHelper.showToast("计算错误！" + e.toString());
    }
  }

  String getStr(double xx) {
    return Helper.RoundDouble(xx, 1, " 万m3");
  }

  String getStr2(double xx) {
    return Helper.RoundDouble(xx, 2, " m");
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    list2.add(InputWidget.getInput("水库水位", c1));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInput("冲沙洞开度", c3));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInput("泄洪洞开度", c2));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInput("机组总出力", c4));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getButton("计算", setQ));

    list1.add(InputWidget.inCard2(list2));
    if (!ifOK) return list1;

    var showColor1 = Colors.blue;
    var v1 = InputWidget.getShowInfo(
        "冲沙洞流量", Helper.RoundDouble(x1.CSD, 0, ""), showColor1);
    var v2 = InputWidget.getShowInfo(
        "泄洪洞流量", Helper.RoundDouble(x1.XHD, 0, ""), showColor1);
    var v3 = InputWidget.getShowInfo(
        "发电流量", Helper.RoundDouble(x1.JZ, 0, ""), showColor1);
    var v4 = InputWidget.getShowInfo(
        "总泄流量", Helper.RoundDouble(x1.CSD + x1.XHD + x1.JZ, 0, ""), showColor1);

    list3.add(InputWidget.getShowRow([v1, v2], true));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getShowRow([v3, v4], true));

    if (ifOK) {
      list1.add(InputWidget.inCard2(list3));
    }

    return list1;
  }

  ResXLNLBody(BuildContext context) => SingleChildScrollView(
        child: ResXLNLFields(context),
      );

  ResXLNLFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
